package user;

import Dao.communityDao;
import Dao.communityDaoImpl;
import been.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet(value = "/editUser")
public class editUserServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        communityDao dao = new communityDaoImpl();
        String message=null;
        int user_id = 0;
        String user_idParam=request.getParameter("user_id");
        if(user_idParam==null||user_idParam.equals("")){
            message = "用户ID不能为空";
            request.setAttribute("message", message);
            request.getRequestDispatcher("/WEB-INF/user/editUser.jsp").forward(request, response);
        }
        else {
            try {
                user_id = Integer.parseInt(user_idParam);
                User user = dao.findUserById(user_id);
                request.setAttribute("user", user);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            request.getRequestDispatcher("/WEB-INF/user/editUser.jsp").forward(request, response);
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        communityDao dao = new communityDaoImpl();
        String message = null;
        int user_id=0;
        try {
            String idParam = request.getParameter("user_id");
            if (idParam == null || idParam.isEmpty()) {
                message = "用户ID不能为空";
                request.setAttribute("message", message);
                request.getRequestDispatcher("/WEB-INF/user/editUser.jsp").forward(request, response);
                return;
            }
            else{
                user_id = Integer.parseInt(idParam);
            }
            String name = request.getParameter("user_name");
            String password = request.getParameter("password");
            String role = request.getParameter("role");

            User user = new User(user_id, name, password, role);
            int res = dao.updateUser(user);

            if (res > 0) {
                message = "修改成功";
                User updatedUser = dao.findUserById(user_id);
                request.setAttribute("user", updatedUser);
            } else {
                message = "修改失败";
                User originalUser = dao.findUserById(user_id);
                request.setAttribute("user", originalUser);
            }

        } catch (NumberFormatException e) {
            message = "用户ID格式错误";
            e.printStackTrace();
        } catch (SQLException e) {
            message = "数据库操作失败";
            e.printStackTrace();
        } catch (Exception e) {
            message = "系统错误：" + e.getMessage();
            e.printStackTrace();
        }
        request.setAttribute("message", message);
        request.getRequestDispatcher("/WEB-INF/user/editUser.jsp").forward(request, response);
    }
}
